<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">
	function addFriend(userid) {
		if(userid == '${userSession.id}') {
			$.messager.alert('提示', '不能填加自己为好友！');
			return;
		}
		$.ajax({
			type : "post",
			dataType : "json",
			url : "${pageContext.request.contextPath}/friendAction!isFriend.action",
			data : "friendid=" + userid,
			beforeSend : function() {
				$.messager.progress({
					text : "正在加载数据，请等待..."
				});
			},
			error : function(info) {
				$.messager.progress('close');
				eval(info.responseText);
			},
			success : function(ret) {
				if (ret.success) {
					$('<div/>').dialog({
						href : '${pageContext.request.contextPath}/app/user/talk/add_user_info.jsp',
						width : 300,
						height : 150,
						modal : true,
						title : '添加好友',
						buttons : [ {
							text : '添加',
							iconCls : 'icon-save',
							handler : function() {
								var groupid = $('#add_user_group').combobox("getValue");
								var remarkName = $("#add_user_remarkName").val();
								var d = $(this).closest('.window-body');
								$.ajax({
									type : "post",
									dataType : "json",
									url : "${pageContext.request.contextPath}/friendAction!addFriend.action",
									data : "friendid=" + userid + "&groupid=" + groupid + "&pfriendExtend.remarkName=" + remarkName,
									beforeSend : function() {
										var isValid = $("#user-userinfo-form").form('validate');
										$.messager.progress({
											text : "提交数据，请等待..."
										});
										return isValid;
									},
									error : function(info) {
										$.messager.progress('close');
										$.messager.show({
											msg : '添加好友失败！',
											title : '提示'
										});
										eval(info.responseText);
									},
									success : function(treeNode) {
										if (treeNode) {
											d.dialog('destroy');
											addContextToTree(treeNode, groupid);
										} else {
											$.messager.show({
												msg : '添加好友失败！',
												title : '提示'
											});
										}
										$.messager.progress('close');
									}
								});
							}
						} ],
						onClose : function() {
							$(this).dialog('destroy');
						}
					});
				} else {
					$.messager.alert('错误', ret.msg);
				}
				$.messager.progress('close');
			}
		});
	}

	//查询功能
	function searchUserItem(value) {
		if ('' == value) {
			$.messager.show({
				msg : '请输入对方的用户名或昵称后再进行查询！',
				title : '提示'
			});
			return;
		}
		$("#user-search-user-view-dg").datagrid("options").pagination = true;
		$("#user-search-user-view-dg").datagrid("options").url = "${pageContext.request.contextPath }/userAction!searchUserItemByName.action?displayName=" + value;
		autoLoad($("#user-search-user-view-dg").datagrid("getPager"), 1);
	}

	$(function() {
		$('#user-search-user-view-dg').datagrid({
			fitColumns : true,
			singleSelect : true,
			pagination : true,
			pageSize : 10,
			pageList : [ 10 ],
			nowrap : true,
			border : false,
			idField : 'id',
			columns : [ [ {
				field : 'name',
				title : '用户名',
				width : 150
			}, {
				field : 'displayName',
				title : '昵称',
				width : 180,
				formatter : function(value) {
					if (value && 'null' != value) {
						return value;
					} else {
						return "无昵称";
					}
				}
			}, {
				field : 'id',
				title : '操作',
				width : 40,
				align : 'center',
				formatter : function(value) {
					if (value) {
						return "<a href='javascript:void(0)' onclick='addFriend(\"" + value + "\")'><img src='${pageContext.request.contextPath }/images/add.png' border='0'></a>";
					}
				}
			} ] ]
		});
	});
</script>
<div id="search-user-dialog-frame" class="easyui-dialog" title="查找联系人" data-options="iconCls:'icon-search', resizable:false, modal:false, closed:true" style="width:400px;height:450px;">
	<table style="width: 100%; height: 100%">
		<tr>
			<td align="center" style="height: 50px"><input id="ss" class="easyui-searchbox" style="width:300px" data-options="searcher:searchUserItem,prompt:'请输入对方的用户名或昵称'"></input></td>
		</tr>
		<tr>
			<td valign="top">
				<table id="user-search-user-view-dg"></table></td>
		</tr>
	</table>

</div>


